package com.cyc.gulimall.cart.controller;

import com.cyc.gulimall.cart.interceptor.CartInterceptor;
import com.cyc.gulimall.cart.service.CartService;
import com.cyc.gulimall.cart.vo.CartItemVo;
import com.cyc.gulimall.cart.vo.CartVo;
import com.cyc.gulimall.cart.vo.UserInfoVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpSession;

@Controller
public class CartController {

    @Autowired
    CartService cartService;


    @GetMapping("/checkItem")
    public String checkItem(@RequestParam("skuId") Long skuId,@RequestParam("checked")Integer checked){

        cartService.checkItem(skuId,checked);

        return "redirect:http://localhost:30000/cart.html";
    }

    @GetMapping("/countItem")
    public String countItem(@RequestParam("skuId") Long skuId,@RequestParam("num")Integer num){

        cartService.countItem(skuId,num);

        return "redirect:http://localhost:30000/cart.html";
    }

    @GetMapping("/deleteItem")
    public String deleteItem(@RequestParam("skuId") Long skuId){

        cartService.deleteItem(skuId);

        return "redirect:http://localhost:30000/cart.html";
    }


    @GetMapping("/cart.html")
    public String cartPage(Model model) {


        CartVo cart=cartService.getCart();
        model.addAttribute("cart",cart);
        return "cartList";
    }


    //添加商品
    @GetMapping("/addCartItem")
    public String addCartItem(@RequestParam("skuId") Long skuId, @RequestParam("num") Integer num, Model model){

        CartItemVo cartItemVo=cartService.addToCart(skuId,num);


        model.addAttribute("cartItem",cartItemVo);

        return "success";
    }
}
